﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    NewsDao nd = new NewsDao();
    AdverDao ad = new AdverDao();
    ShopDao sd = new ShopDao();
    VideoDao vd = new VideoDao();
    HouseDao hd = new HouseDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            dt = ad.getAdver();
            HiddenField1.Value = "";
            HiddenField2.Value = "";
            if (dt!=null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < 5; i++)
                {
                    if (i == 0)
                    {
                        HiddenField1.Value = dt.Rows[i][1].ToString().Replace("../","");
                        HiddenField2.Value = dt.Rows[i][2].ToString();
                    }
                    else
                    {
                        HiddenField1.Value = HiddenField1.Value + "|" + dt.Rows[i][1].ToString().Replace("../", "");
                        HiddenField2.Value = HiddenField2.Value + "|" + dt.Rows[i][2].ToString();
                    }
                }
                hlcenter.ImageUrl = dt.Rows[5][1].ToString().Replace("../", "");
                hlcenter.NavigateUrl = dt.Rows[5][2].ToString();
                hlorigin.ImageUrl = dt.Rows[6][1].ToString().Replace("../", "");
                hlorigin.NavigateUrl = dt.Rows[6][2].ToString();
                hlview.ImageUrl = dt.Rows[7][1].ToString().Replace("../", "");
                hlview.NavigateUrl = dt.Rows[7][2].ToString();
                hledu.ImageUrl = dt.Rows[8][1].ToString().Replace("../", "");
                hledu.NavigateUrl = dt.Rows[8][2].ToString();
                hlpicping.ImageUrl = dt.Rows[9][1].ToString().Replace("../", "");
                hlpicping.NavigateUrl = dt.Rows[9][2].ToString();
                hlpicjoin.ImageUrl = dt.Rows[10][1].ToString().Replace("../", "");
                hlpicjoin.NavigateUrl = dt.Rows[10][2].ToString();
                hlpicclass.ImageUrl = dt.Rows[11][1].ToString().Replace("../", "");
                hlpicclass.NavigateUrl = dt.Rows[11][2].ToString();
            }
            SetNewsTop();
            SetNews("1", "9", dlbus, 13);
            SetNews("2", "10", dlnews, 20);
            SetNews("3", "10", dlorigin, 18);
            SetNews("4", "10", dlview, 24);
            SetNews("5", "14", dledu, 15);
            SetAboutNews("6", "12", "10", dlping, 19);
            SetAboutNews("6", "13", "10", dljoin, 19);
            SetAboutNews("6", "14", "10", dlclass, 19);
            SetShop();
            SetView();
            SetHouse();
        }
    }

    //加载新闻头条信息
    public void SetNewsTop()
    {
        dt = nd.getNewsTop();
        if (dt != null && dt.Rows.Count > 1)
        {
            hltop1.Text = dt.Rows[0][1].ToString();
            string id = dt.Rows[0][0].ToString();
            id = em.Encript(id);
            hltop1.NavigateUrl = string.Format("Main/News.aspx?nid={0}", id);
            if (hltop1.Text.Length > 15)
            {
                hltop1.Text = hltop1.Text.Substring(0, 15) + "...";
            }
            hltop2.Text = dt.Rows[1][1].ToString();
            id = dt.Rows[1][0].ToString();
            id = em.Encript(id);
            hltop2.NavigateUrl = string.Format("Main/News.aspx?nid={0}", id);
            if (hltop2.Text.Length > 15)
            {
                hltop2.Text = hltop2.Text.Substring(0, 15) + "...";
            }
        }
    }

    //加载新闻信息
    public void SetNews(string tid, string count, DataList dl,int len)
    {
        dt = nd.getNews(tid, count);
        if (dt != null && dt.Rows.Count > 0)
        {
            dl.DataSource = dt;
            dl.DataBind();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i][0].ToString();
                id = em.Encript(id);
                HyperLink hl = (HyperLink)dl.Items[i].FindControl("hlnews");
                while (hl.ToolTip.IndexOf("<") >= 0 && hl.ToolTip.IndexOf(">") >= 0)
                {
                    int start = hl.ToolTip.IndexOf("<");
                    int end = hl.ToolTip.IndexOf(">");
                    if (start > end)
                    {
                        hl.ToolTip = hl.ToolTip.Replace(">", "");
                    }
                    else
                    {
                        string rep = hl.ToolTip.Substring(start, end - start + 1);
                        hl.ToolTip = hl.ToolTip.Replace(rep, "");
                    }
                    hl.ToolTip = hl.ToolTip.Replace("&nbsp;", "");
                    hl.ToolTip = hl.ToolTip.Replace("&#8212;", "—");
                    hl.ToolTip = hl.ToolTip.Replace("&#8216;", "‘");
                    hl.ToolTip = hl.ToolTip.Replace("&#8217;", "’");
                    hl.ToolTip = hl.ToolTip.Replace("&#8220;", "“");
                    hl.ToolTip = hl.ToolTip.Replace("&#8221;", "”");
                    hl.ToolTip = hl.ToolTip.Replace("&#8230;", "…");
                    hl.ToolTip = hl.ToolTip.Replace("{$Page$}", "(图)");
                    hl.ToolTip = hl.ToolTip.Replace("\r\n\r\n", "…");
                }
                if (hl.ToolTip.Length > 200)
                {
                    hl.ToolTip = hl.ToolTip.Substring(0, 200) + "......";
                }
                if (hl.Text.Length > len)
                {
                    hl.Text = hl.Text.Substring(0, len) + "...";
                }
                hl.NavigateUrl = string.Format("Main/News.aspx?nid={0}", id);
            }
        }
    }

    //加载说三道四信息
    public void SetAboutNews(string tid,string mid, string count, DataList dl, int len)
    {
        dt = nd.getAboutNews(tid,mid, count);
        if (dt != null && dt.Rows.Count > 0)
        {
            dl.DataSource = dt;
            dl.DataBind();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i][0].ToString();
                id = em.Encript(id);
                HyperLink hl = (HyperLink)dl.Items[i].FindControl("hlnews");
                while (hl.ToolTip.IndexOf("<") >= 0 && hl.ToolTip.IndexOf(">") >= 0)
                {
                    int start = hl.ToolTip.IndexOf("<");
                    int end = hl.ToolTip.IndexOf(">");
                    if (start > end)
                    {
                        hl.ToolTip = hl.ToolTip.Replace(">", "");
                    }
                    else
                    {
                        string rep = hl.ToolTip.Substring(start, end - start + 1);
                        hl.ToolTip = hl.ToolTip.Replace(rep, "");
                    }
                    hl.ToolTip = hl.ToolTip.Replace("&nbsp;", "");
                    hl.ToolTip = hl.ToolTip.Replace("&#8212;", "—");
                    hl.ToolTip = hl.ToolTip.Replace("&#8216;", "‘");
                    hl.ToolTip = hl.ToolTip.Replace("&#8217;", "’");
                    hl.ToolTip = hl.ToolTip.Replace("&#8220;", "“");
                    hl.ToolTip = hl.ToolTip.Replace("&#8221;", "”");
                    hl.ToolTip = hl.ToolTip.Replace("&#8230;", "…");
                    hl.ToolTip = hl.ToolTip.Replace("{$Page$}", "(图)");
                    hl.ToolTip = hl.ToolTip.Replace("\r\n\r\n", "…");
                }
                if (hl.ToolTip.Length > 200)
                {
                    hl.ToolTip = hl.ToolTip.Substring(0, 200) + "......";
                }
                if (hl.Text.Length > len)
                {
                    hl.Text = hl.Text.Substring(0, len) + "...";
                }
                hl.NavigateUrl = string.Format("Main/News.aspx?nid={0}", id);
            }
        }
    }

    //加载小店信息
    private void SetShop()
    {
        dt = sd.getIndexShop(10);
        if (dt != null && dt.Rows.Count > 0)
        {
            dlshop.DataSource = dt;
            dlshop.DataBind();
            for (int i = 0; i < dlshop.Items.Count; i++)
            {
                HyperLink hlshop = dlshop.Items[i].FindControl("hlshop") as HyperLink;
                Label labSname = dlshop.Items[i].FindControl("labSname") as Label;
                Image imgbtnshop = dlshop.Items[i].FindControl("imgbtnshop") as Image;
                imgbtnshop.ImageUrl = imgbtnshop.ImageUrl.Replace("../","");
                string sid = em.Encript(dt.Rows[i][0].ToString());
                hlshop.NavigateUrl = "Main/ShopInfo.aspx?sid=" + sid;
                if (labSname.Text.Length > 10)
                {
                    labSname.Text = labSname.Text.Substring(0, 10) + "...";
                }
            }
        }
    }

    //加载视频信息
    private void SetView()
    {
        dt = vd.getVideo("5");
        if (dt != null && dt.Rows.Count > 0)
        {
            dlvideo.DataSource = dt;
            dlvideo.DataBind();
            for (int i = 0; i < dlvideo.Items.Count; i++)
            {
                HyperLink hlvideo = dlvideo.Items[i].FindControl("hlvideo") as HyperLink;
                Label labVname = dlvideo.Items[i].FindControl("labVname") as Label;
                Image imgvideo = dlvideo.Items[i].FindControl("imgvideo") as Image;
                imgvideo.ImageUrl = imgvideo.ImageUrl.Replace("../", "");
                string vid = em.Encript(dt.Rows[i][0].ToString());
                hlvideo.NavigateUrl = "Main/VideoInfo.aspx?vid=" + vid;
                if (labVname.Text.Length > 10)
                {
                    labVname.Text = labVname.Text.Substring(0, 10) + "...";
                }
            }
        }
    }

    //加载楼盘信息
    private void SetHouse()
    {
        dt = hd.getIndexHouse(5);
        if (dt != null && dt.Rows.Count > 0)
        {
            dlhouse.DataSource = dt;
            dlhouse.DataBind();
            for (int i = 0; i < dlhouse.Items.Count; i++)
            {
                HyperLink hlhouse = dlhouse.Items[i].FindControl("hlhouse") as HyperLink;
                Label labHname = dlhouse.Items[i].FindControl("labHname") as Label;
                Image imgbtnhouse = dlhouse.Items[i].FindControl("imgbtnhouse") as Image;
                imgbtnhouse.ImageUrl = imgbtnhouse.ImageUrl.Replace("../", "");
                string hid = em.Encript(dt.Rows[i][0].ToString());
                hlhouse.NavigateUrl = "Main/HouseInfo.aspx?hid=" + hid;
                if (labHname.Text.Length > 10)
                {
                    labHname.Text = labHname.Text.Substring(0, 10) + "...";
                }
            }
        }
    }
}
